/**
 *
 * © Bahadir Zeren 2011
 * bzeren@gmail.com
 * No warranties, express or implied, are made for this program.
 *
 */

package org.baatar.remoting.service;

import java.io.Serializable;
import java.net.InetAddress;

/**
 * Stores information about remoting service.
 * 
 * @author B_ZEREN
 */
public class RemotingServiceInfo implements Serializable {

    private String hostName = null;
    private int listenerPort = 0;
    private String implementorClass = null;
    private String strRepresentation = null;

    private void setRepresentation() {
        strRepresentation = implementorClass + "-" + hostName + ":" + String.valueOf(listenerPort);
    }

    /**
     * Constructor of the RemotingServiceInfo class.
     * 
     * @param hostName IP number of the remote service.
     * @param listenerPort port number of the remote service.
     * @param implementorClass implementor class name of the remote service.
     */
    public RemotingServiceInfo(String hostName,
                        int listenerPort,
                        String implementorClass) {
        this.hostName = hostName;
        this.listenerPort = listenerPort;
        this.implementorClass = implementorClass;
        setRepresentation();
    }

    /**
     * Gets host name of the current machine.
     * 
     * @return String hostname of the current machine.
     */
    private static String getLocalHost() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch(Exception ex) {
            return "unknownLocal";
        }
    }

    /**
     * Constructor of the RemotingServiceInfo class.
     * 
     * @param listenerPort port number of the remote service.
     * @param implementorClass implementor class name of the remote service.
     */
    public RemotingServiceInfo(int listenerPort,
                                String implementorClass) {
        this.hostName = getLocalHost();
        this.listenerPort = listenerPort;
        this.implementorClass = implementorClass;
        setRepresentation();
    }

    /**
     * Constructor of the RemotingServiceInfo class.
     * 
     * @param listenerPort port number of the remote service.
     */
    public RemotingServiceInfo(int listenerPort) {
        this.hostName = getLocalHost();
        this.listenerPort = listenerPort;
        this.implementorClass = "Client";
        setRepresentation();
    }

    /**
     * Returns hostname of the remote service.
     * 
     * @return String hostname.
     */
    public String getHostName() {
        return hostName;
    }

    /**
     * Returns port number of the remote service.
     * 
     * @return int port number.
     */
    public int getListenerPort() {
        return listenerPort;
    }

    /**
     * Returns class name of the implementor class of the remote service.
     * 
     * @return String implementor class name.
     */
    public String getImplementorClass() {
        return implementorClass;
    }

    @Override
    public String toString() {
        return strRepresentation;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 79 * hash + (this.hostName != null ? this.hostName.hashCode() : 0);
        hash = 79 * hash + this.listenerPort;
        hash = 79 * hash + (this.implementorClass != null ? this.implementorClass.hashCode() : 0);
        return hash;
    }

    @Override
    @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
    public boolean equals(Object value) {
        RemotingServiceInfo compareValue = (RemotingServiceInfo) value;
        return (compareValue.getHostName().equals(hostName)
                && compareValue.getImplementorClass().equals(implementorClass)
                && (compareValue.getListenerPort() == listenerPort));
    }

}
